<?php
class Horarios_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }
    
	function getByFilterPaging($objSearch=NULL, $table, $data)
	{
        $this->db->flush_cache();
		$this->db->start_cache();
		if (!is_null($objSearch))
		{
			foreach ($objSearch->rules as $item)
			{
				switch ($item->op)
				{
					case "eq":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." =", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." =", $item->data);
						}
						break;
					case "ne":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." <>", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." <>", $item->data);
						}
						break;
					case "lt":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." <", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." <", $item->data);
						}
						break;
					case "le":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." <=", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." <=", $item->data);
						}
						break;
					case "gt":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." >", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." >", $item->data);
						}
						break;
					case "ge":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." >=", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." >=", $item->data);
						}
						break;
					case "bw":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->like($item->field, $item->data,'after');
						}
						else
						{
							$this->db->or_like($item->field, $item->data,'after');
						}
						break;
					case "bn":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->not_like($item->field, $item->data,'after');
						}
						else
						{
							$this->db->or_not_like($item->field, $item->data,'after');
						}
						break;
					case "in":
						$arr_values = explode(";", $item->data);
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where_in($item->field, $arr_values);
						}
						else
						{
							$this->db->or_where_in($item->field, $arr_values);
						}
						break;
					case "ni":
						$arr_values = explode(";", $item->data);
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where_not_in($item->field, $arr_values);
						}
						else
						{
							$this->db->or_where_not_in($item->field, $arr_values);
						}
						break;
					case "ew":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->like($item->field, $item->data,'before');
						}
						else
						{
							$this->db->or_like($item->field, $item->data,'before');
						}
						break;
					case "en":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->not_like($item->field, $item->data,'before');
						}
						else
						{
							$this->db->or_not_like($item->field, $item->data,'before');
						}
						break;
					case "cn":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->like($item->field, $item->data,'both');
						}
						else
						{
							$this->db->or_like($item->field, $item->data,'both');
						}
						break;
					case "nc":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->not_like($item->field, $item->data,'both');
						}
						else
						{
							$this->db->or_not_like($item->field, $item->data,'both');
						}
						break;
				}
			}
		}
		$page = $data['page'];
		$limit = $data['rows'];
		$sidx = $data['sidx'];
		$sord = $data['sord'];
		
		$this->db->select(" tb_horarios.id AS id,
							tb_medicos.nome AS nome,
							tb_dias_semana.descricao AS dia,
							tb_horarios.horario AS horario,
							tb_horarios.flag_disponivel_site AS flag_disponivel_site");
		$this->db->from("tb_horarios");
		$this->db->join("tb_medicos", "tb_medicos.id = tb_horarios.id_medicos_fk");
		$this->db->join("tb_dias_semana", "tb_dias_semana.id = tb_horarios.id_dias_semana_fk");
		
		$count = $this->db->count_all_results();
		
		if( $count > 0 && $limit > 0) { 
              $total_pages = ceil($count/$limit); 
        } else { 
              $total_pages = 0; 
        } 
		if ($page > $total_pages) $page=$total_pages;
		$start = $limit * $page - $limit;
		if($start <0) $start = 0;
		$this->db->order_by($sidx , $sord);
		
		$this->db->limit($limit, $start);

		$query = $this->db->get($table);
		
		$data_result['result'] = $query->result();
        $data_result['page'] = $page;
        $data_result['totalpages'] = $total_pages;
        $data_result['totalrecords']=$count;

        $this->db->flush_cache();
		return $data_result;
	}

	function getDiasMedico($objSearch = NULL)
	{
        $this->db->flush_cache();
		$this->db->start_cache();
		if (!is_null($objSearch))
		{
			foreach ($objSearch->rules as $item)
			{
				switch ($item->op)
				{
					case "eq":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." =", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." =", $item->data);
						}
						break;
					case "ne":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." <>", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." <>", $item->data);
						}
						break;
					case "lt":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." <", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." <", $item->data);
						}
						break;
					case "le":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." <=", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." <=", $item->data);
						}
						break;
					case "gt":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." >", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." >", $item->data);
						}
						break;
					case "ge":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." >=", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." >=", $item->data);
						}
						break;
					case "bw":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->like($item->field, $item->data,'after');
						}
						else
						{
							$this->db->or_like($item->field, $item->data,'after');
						}
						break;
					case "bn":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->not_like($item->field, $item->data,'after');
						}
						else
						{
							$this->db->or_not_like($item->field, $item->data,'after');
						}
						break;
					case "in":
						$arr_values = explode(";", $item->data);
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where_in($item->field, $arr_values);
						}
						else
						{
							$this->db->or_where_in($item->field, $arr_values);
						}
						break;
					case "ni":
						$arr_values = explode(";", $item->data);
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where_not_in($item->field, $arr_values);
						}
						else
						{
							$this->db->or_where_not_in($item->field, $arr_values);
						}
						break;
					case "ew":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->like($item->field, $item->data,'before');
						}
						else
						{
							$this->db->or_like($item->field, $item->data,'before');
						}
						break;
					case "en":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->not_like($item->field, $item->data,'before');
						}
						else
						{
							$this->db->or_not_like($item->field, $item->data,'before');
						}
						break;
					case "cn":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->like($item->field, $item->data,'both');
						}
						else
						{
							$this->db->or_like($item->field, $item->data,'both');
						}
						break;
					case "nc":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->not_like($item->field, $item->data,'both');
						}
						else
						{
							$this->db->or_not_like($item->field, $item->data,'both');
						}
						break;
					case "ob":
						$this->db->order_by($item->field, $item->data);
						break;
				}
			}
		}

		$this->db->select(" tb_dias_semana.id AS id,
							tb_dias_semana.descricao AS descricao");
		$this->db->from("tb_dias_semana");
		$this->db->join("tb_medicos_dias_semana", "tb_medicos_dias_semana.id_dias_semana_fk = tb_dias_semana.id");
		
		$query = $this->db->get();
		
		$result = $query->result();

        $this->db->flush_cache();

		return $result;	    
	}
}